﻿// <copyright file="ImageExtensionsTest.cs" company="DotNetLib">
//
// .netlib: http://www.codeplex.com/DotNetLib
//
// This code is released under the New BSD License.
// A copy of this license is available at:
// http://www.codeplex.com/DotNetLib/license
//
// </copyright>

namespace DotNetLib.Tests
{
  using System;
  using System.Drawing;
  using System.Drawing.Imaging;
  using System.IO;
  using DotNetLib.Drawing;
  using global::Microsoft.VisualStudio.TestTools.UnitTesting;

  /// <summary>
  /// This is a test class for DotNetLib.Drawing.Images.
  /// </summary>
  [TestClass()]
  public class ImageExtensionsTest : TestBase
  {
    private const string Filename = @"Sample.png";

    [ClassInitialize()]
    public static void Initialize(TestContext testContext)
    {
      DotNetLib.Tests.Properties.Resources.SampleImage.Save(Filename, ImageFormat.Png);
    }

    [ClassCleanup()]
    public static void Cleanup()
    {
      File.Delete(Filename);
    }

    /// <summary>
    /// A test for ToByteArray
    /// </summary>
    [TestMethod()]
    public void ToByteArrayTest()
    {
      using (Image original = Image.FromFile(Filename))
      {
        byte[] bytes = original.ToByteArray();
        FileInfo fileInfo = new FileInfo(Filename);
        Assert.AreEqual(fileInfo.Length, bytes.Length);
      }
    }

    /// <summary>
    /// A test for FromByteArray
    /// </summary>
    [TestMethod()]
    [ExpectedException(typeof(ArgumentNullException))]
    public void ToByteArrayTestArgumentNull()
    {
      ImageExtensions.ToByteArray(null);
    }

    /// <summary>
    /// A test for FromByteArray
    /// </summary>
    [TestMethod()]
    public void ToByteArrayTest_1()
    {
      using (Image original = Image.FromFile(Filename))
      {
        byte[] bytes = original.ToByteArray(ImageFormat.Png);
        FileInfo fileInfo = new FileInfo(Filename);
        Assert.AreEqual(fileInfo.Length, bytes.Length);
      }
    }

    /// <summary>
    /// A test for FromByteArray
    /// </summary>
    [TestMethod()]
    [ExpectedException(typeof(ArgumentNullException))]
    public void ToByteArrayTest_1ArgumentNull()
    {
      ImageExtensions.ToByteArray(null, ImageFormat.Png);
    }

    /// <summary>
    /// A test for FromByteArray
    /// </summary>
    [TestMethod()]
    public void FromByteArrayTest()
    {
      using (Image original = ImageExtensions.FromByteArray(File.ReadAllBytes(Filename)))
      {
        Assert.IsNotNull(original);
      }
    }

    /// <summary>
    /// A test for FromByteArray
    /// </summary>
    [TestMethod()]
    [ExpectedException(typeof(ArgumentNullException))]
    public void FromByteArrayTestArgumentNull()
    {
      ImageExtensions.FromByteArray(null);
    }

    /// <summary>
    /// A test for AlphaBlend
    /// </summary>
    [TestMethod()]
    public void ToAlphaBlendTest()
    {
      using (Image original = Image.FromFile(Filename))
      using (Image result = original.ToAlphaBlend(128, Color.Red))
      {
        Assert.IsNotNull(result);
      }
    }

    /// <summary>
    /// A test for ToGrayscale
    /// </summary>
    [TestMethod()]
    public void ToGrayscaleTest()
    {
      using (Image original = Image.FromFile(Filename))
      using (Image result = original.ToGrayscale())
      {
        Assert.IsNotNull(result);
      }
    }

    /// <summary>
    /// A test for Resize
    /// </summary>
    [TestMethod()]
    public void ResizeTest()
    {
      // limit by width
      using (Image original = Image.FromFile(Filename))
      using (Image result = original.Resize(100, 200, false))
      {
        Assert.IsNotNull(result);
        Assert.AreEqual(134, result.Height);
        Assert.AreEqual(100, result.Width);
      }

      // limit by height
      using (Image original = Image.FromFile(Filename))
      using (Image result = original.Resize(200, 100, false))
      {
        Assert.IsNotNull(result);
        Assert.AreEqual(100, result.Height);
        Assert.AreEqual(75, result.Width);
      }

      // test padding
      using (Image original = Image.FromFile(Filename))
      using (Image result = original.Resize(100, 100, true))
      {
        Assert.IsNotNull(result);
        Assert.AreEqual(100, result.Height);
        Assert.AreEqual(100, result.Width);
      }
    }

    /// <summary>
    /// A test for FromByteArray
    /// </summary>
    [TestMethod()]
    [ExpectedException(typeof(ArgumentNullException))]
    public void ResizeTest_1ArgumentNull()
    {
      ImageExtensions.Resize(null, 100, 100, false);
    }
  }
}
